import {
  Column,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

import {
  Exclude,
  // Expose
} from 'class-transformer';
import { ApiProperty } from '@nestjs/swagger';

import { TagEntity } from '../../tag/entities/tag.entity';
import { CategoryEntity } from '../../category/entities/category.entity';
import { User } from 'src/modules/user/entities/user.entity';

@Entity('posts') //通过@Entity()装饰的class 映射为数据表
export class PostsEntity {
  @PrimaryGeneratedColumn('uuid')
  id: number; // 标记为主列，值自动生成

  @ApiProperty({ description: '文章标题' })
  @Column({ length: 50 })
  title: string;

  @ApiProperty({ description: 'markdown内容' })
  @Column({ type: 'mediumtext', default: null })
  content: string;

  @ApiProperty({ description: 'markdown 转 html,自动生成' })
  @Column({ type: 'mediumtext', default: null, name: 'content_html' })
  contentHtml: string;

  @ApiProperty({ description: '摘要，自动生成' })
  @Column({ type: 'text', default: null })
  summary: string;

  @ApiProperty({ description: '封面图' })
  @Column({ default: null, name: 'cover_url' })
  coverUrl: string;

  @ApiProperty({ description: '阅读量' })
  @Column({ type: 'int', name: 'view_count', default: 0 })
  viewCount: number;

  @ApiProperty({ description: '点赞量' })
  @Column({ type: 'int', default: 0, name: 'like_count' })
  likeCount: number;

  @ApiProperty({ description: '评论数量' })
  @Column({ comment: '评论数量', default: 0, name: 'comment_count' })
  commentCount: number;

  @ApiProperty({ description: '收藏数量' })
  @Column({
    type: 'int',
    comment: '收藏数量',
    name: 'favorite_count',
    default: 0,
  })
  favoriteCount: number;

  @ApiProperty({ description: '推荐显示' })
  @Column({ type: 'tinyint', default: 0, name: 'is_recommend' })
  isRecommend: number;

  @ApiProperty({ description: '文章状态' })
  @Column('simple-enum', { enum: ['draft', 'publish'] })
  status: string;

  @ApiProperty({ description: '作者' })
  @ManyToOne(() => User, (user) => user.nickname)
  author: User;

  //   @RelationId( (user:User) => user.posts)
  //   userId:User

  @ApiProperty({ description: '分类' })
  @Exclude()
  @ManyToOne(() => CategoryEntity, (category) => category.posts, {
    // cascade: true,
  })
  @JoinColumn({
    name: 'category_id',
  })
  category: CategoryEntity;

  @ApiProperty({ description: '标签' })
  @ManyToMany(() => TagEntity, (tag) => tag.posts)
  @JoinTable({
    name: 'post_tag',
    joinColumns: [{ name: 'post_id' }],
    inverseJoinColumns: [{ name: 'tag_id' }],
  })
  tags: TagEntity[];

  @Column({ type: 'timestamp', name: 'publish_time', default: null })
  publishTime: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  create_time: Date;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  update_time: Date;
}
